Set Up

  • Install/load necessary R packages
  • Set working directory if necessary (or create a file path to use throughout the RMD to call the data from Box)
  • Set constant values for reused variables

Streamflow compared to cumulative precipitation and manual checkpoints

Cumulative Precipitation Every 6 Hours

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_6h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_6h <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_6h_clean, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = "S2 Bog Streamflow and Cumulative Precipitation Every 6 Hours",
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
  )

#static plot
#p_6h

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_6h_mc.png",
       plot = p_6h,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_6h)

Cumulative Precipitation Every 12 Hours

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_12h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_12h <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_12h_clean, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = "S2 Bog Streamflow and Cumulative Precipitation Every 12 Hours",
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    ###for example, max value of precipitation / max value of streamflow = 0.533...
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
  )

#static plot
#p_12h

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_12h_mc.png",
       plot = p_12h,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_12h)

Cumulative Daily Precipitation

#format precipitation data for a smooth joining process
precip_daily_join <- precip_daily_clean %>% 
  pivot_wider(
    names_from = "precip_type",
    values_from = "precip_in"
  ) %>% 
  subset(year >= min_year & year <= max_year) %>% 
  rename(precip_10_in = precip_10) %>% 
  mutate(datetime = format(as.POSIXct(date, format = '%Y-%m-%d', 
                                  tz = "GMT"),
                           format = '%Y-%m-%d %H:%M:%S'),
         datetime = as.POSIXct(datetime))

#join streamflow and daily precipitation data 
streamflow_precip_daily <- full_join(x = all_streamflow, 
                                     y = precip_daily_join, 
                                     by = "date") %>% 
  select(-precip_in) %>% 
  subset(year >= min_year & year <= max_year)

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_daily_join$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_daily <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_daily_join, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = "S2 Bog Streamflow and Daily Cumulative Precipitation",
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    ###for example, max value of precipitation / max value of streamflow = 0.533...
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Daily Precipitation / 10 (in)")
  )

#static plot
#p_daily

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_daily_mc.png",
       plot = p_daily,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_daily)

Streamflow + air temperature data (every 30min) + manual checks

#format air temperature data for plotting 
airtemp_join <- air_temp_30_clean %>% 
  pivot_wider(
    names_from = "airtemp_type",
    values_from = "airtempC"
  ) %>% 
  subset(year >= min_year & year <= max_year) %>% 
  mutate(freezing = as.factor(freezing))

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(airtemp_join$airtempc_100, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_air <- ggplot() + 
  #airtemp data
  geom_line(data = airtemp_join, 
            aes(x = datetime,
                y = airtempc_100),
            color = "pink", 
            alpha = 0.4) +
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.75) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, 
                 y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = "S2 Bog Streamflow and Air Temperature (2017 - 2019)",
       subtitle = "Air temperature is plotted in pink. \n Streamflow is plotted in blue. \n Manual streamflow checkpoints are plotted as black dots.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) + 
  scale_y_continuous(
    # Features of the first axis
    name = "Air Temperature / 100 (ÂșC)",
    # Add a second axis and specify its features
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Stream Height (ft)")
  ) 

#static plot
#p_air

#save the plot in the figures folder 
ggsave(filename = "streamflow_airtemp_mc.png",
       plot = p_air,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot
ggplotly(p_air)